/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bff.slimserver.musicjukebox.ui.components;

/**
 *
 * @author Bill
 */
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;

public class TestList extends JFrame implements MouseListener {

    public static void main(String args[]) {
        TestList vTest = new TestList();
        vTest.pack();
        vTest.setVisible(true);
    }

    public TestList() {
        super("Active button render");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        String[] vData = new String[]{"one", "two", "three", "four", "five"};
        JList vList = new JList(vData);
        vList.setCellRenderer(new MyRenderer());
        vList.addMouseListener(this);
        getContentPane().add(vList);
    }

    public class MyRenderer extends JPanel implements ListCellRenderer {

        JLabel wLabel = new JLabel();
        JButton wButton = new JButton();
        JButton wButton1 = new JButton();

        public MyRenderer() {
            setLayout(new FlowLayout());
            add(wLabel);
            add(wButton);
            add(wButton1);
        }

        public Component getListCellRendererComponent(JList aList, Object aValue, int aIndex, boolean aIsSelected,
                boolean aCellHasFocus) {
            wLabel.setText("Label for button " + aValue.toString());
            wButton.setText(aValue.toString());
            wButton1.setText("another botton for " + aValue.toString());
            return this;
        }
    }

    public void mouseClicked(MouseEvent aE) {
        JList lstCust = (JList) aE.getSource();
        int currIndex = lstCust.locationToIndex(aE.getPoint());
        String currData = (String) lstCust.getSelectedValue();
        Rectangle itemBounds = lstCust.getCellBounds(currIndex, currIndex);
        Point currPoint = new Point(aE.getX(), aE.getY());
        Component readyComponent = ((MyRenderer) lstCust.getCellRenderer()).getListCellRendererComponent(lstCust,
                currData, currIndex, true, true);
        readyComponent.setSize(itemBounds.width, itemBounds.height);
        Component pressedComponent = null;
        while (readyComponent != pressedComponent) {
            if (pressedComponent != null) {
                readyComponent = pressedComponent;
            }
            pressedComponent = readyComponent.getComponentAt((int) (currPoint.getX() - itemBounds.getX()),
                    (int) (currPoint.getY() - itemBounds.getY()));
            if (pressedComponent == null) {
                System.out.println("Pressed component = null loop");
                return;
            }
            currPoint.setLocation(currPoint.getX() - itemBounds.getX(), currPoint.getY() - itemBounds.getY());
            itemBounds = pressedComponent.getBounds();
        }

        if (pressedComponent instanceof JButton) {
            System.out.println("Pressed JBotton with text " + ((JButton) pressedComponent).getText() + " in row " + currIndex + " of JList");
        }
    }

    public void mouseEntered(MouseEvent aE) {
    }

    public void mouseExited(MouseEvent aE) {
    }

    public void mousePressed(MouseEvent aE) {
    }

    public void mouseReleased(MouseEvent aE) {
    }
} 
